//package com.example.lsrsbackend.service.impl;
//
//import com.example.lsrsbackend.model.DTO.SeatStatusDTO;
//import com.example.lsrsbackend.service.RedisService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//
//public class RedisServiceImpl implements RedisService {
////    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
//
//    private static final String SEAT_KEY_PREFIX = "seat:status:";
//    private static final String SEAT_LOCK_PREFIX = "lock:seat:";
//    private static final String SEAT_CHANNEL = "seat_status_channel";
//
//    @Value("${library.seat.cache.seat-status-ttl:3600}")
//    private long seatStatusTtl;
//
//    @Override
//    public SeatStatusDTO getSeatStatus(String seatId) {
//        String key = SEAT_KEY_PREFIX + seatId;
//        return (SeatStatusDTO) redisTemplate.opsForValue().get(key);
//    }
//
//    @Override
//    public void updateSeatStatus(SeatStatusDTO status) {
//        String key = SEAT_KEY_PREFIX + status.getSeatId();
//        redisTemplate.opsForValue().set(key, status, Duration.ofSeconds(seatStatusTtl));
//    }
//
//    @Override
//    public boolean tryLockSeat(String seatId, String userId) {
//        String lockKey = SEAT_LOCK_PREFIX + seatId;
//        String lockValue = userId + ":" + System.currentTimeMillis();
//
//        return Boolean.TRUE.equals(
//                redisTemplate.opsForValue().setIfAbsent(
//                        lockKey, lockValue, Duration.ofSeconds(10))
//        );
//    }
//
//    @Override
//    public void unlockSeat(String seatId, String userId) {
//        String lockKey = SEAT_LOCK_PREFIX + seatId;
//        String currentValue = (String) redisTemplate.opsForValue().get(lockKey);
//
//        if (currentValue != null && currentValue.startsWith(userId)) {
//            redisTemplate.delete(lockKey);
//        }
//    }
//
//    @Override
//    public void publishSeatStatusChange(SeatStatusDTO status) {
//        redisTemplate.convertAndSend(SEAT_CHANNEL, status);
//    }
//}
